﻿Imports lbGlobalcom
Imports Oracle.DataAccess.Client
Public Class fFacturacionPlanes
    Dim daPlanes As lbGlobalcom.dsProductosTableAdapters.PRO_PLANESTableAdapter
    Private Sub fFacturacionPlanes_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            dtpFechaHasta.Value = Date.Now.Date
            dtpFechaDesde.Value = dtpFechaHasta.Value.AddDays(-30)
            daPlanes = New lbGlobalcom.dsProductosTableAdapters.PRO_PLANESTableAdapter
            daPlanes.Fill(DsProductos.PRO_PLANES)
            Dim drPla As dsProductos.PRO_PLANESRow = DsProductos.PRO_PLANES.NewPRO_PLANESRow
            drPla.IDPLAN = "00000"
            drPla.DESCRIPCION = "Todas"
            DsProductos.PRO_PLANES.AddPRO_PLANESRow(drPla)
            cbPlanes.SelectedValue = "00000"
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnConsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsultar.Click
        Dim conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
        Dim cmd As OracleCommand = conn.CreateCommand
        Dim qWhere As String = " WHERE MOV_PLAN.IDDOCUMENTO=MOV_PLANDETALLE.IDDOCUMENTO(+) AND MOV_PLAN.NUMERO=MOV_PLANDETALLE.NUMERO(+) AND MOV_PLAN.IDTERCERO = TER_CLIENTES.IDCLIENTE(+) AND MOV_PLAN.SUCURSAL=TER_CLIENTES.SUCURSAL(+) AND  TRUNC(MOV_PLAN.FECHADOCUMENTO) BETWEEN :PDESDEF AND :PHASTAF AND MOV_PLANDETALLE.ACTIVO='A'"
        Dim qSelect As String = "SELECT MOV_PLAN.IDDOCUMENTO,MOV_PLAN.NUMERO,MOV_PLAN.IDTERCERO,MOV_PLAN.SUCURSAL,MOV_PLANDETALLE.IDPLAN,MOV_PLANDETALLE.NUMEROLINEA,MOV_PLANDETALLE.IDPRODUCTOIMEI,MOV_PLANDETALLE.IMEI,MOV_PLANDETALLE.VALORBASEIMEI,MOV_PLANDETALLE.VALORIVAIMEI,MOV_PLANDETALLE.IDPRODUCTOICCID,MOV_PLANDETALLE.ICCID,MOV_PLANDETALLE.VALORBASEICCID,MOV_PLANDETALLE.VALORIVAICCID,MOV_PLANDETALLE.MIND,MOV_PLANDETALLE.VALOR VALORPLAN,TER_CLIENTES.NOMBREESTABLECIMIENTO FROM MOV_PLAN, MOV_PLANDETALLE,TER_CLIENTES "
        Try
            If txtDocumento.Text.Trim.Length > 0 Then
                qWhere &= " AND MOV_PLAN.IDDOCUMENTO='" & txtDocumento.Text.Trim & "'"
            End If
            If txtNumero.Text.Trim.Length > 0 Then
                qWhere &= " AND MOV_PLAN.NUMERO= " & txtNumero.Text.Trim
            End If
            If cbPlanes.SelectedValue <> "00000" Then
                qWhere &= " AND MOV_PLANDETALLE.IDPLAN ='" & cbPlanes.SelectedValue & "'"
            End If

            cmd.CommandText = qSelect & qWhere
            cmd.Parameters.Add("PDESDEF", OracleDbType.Date).Value = dtpFechaDesde.Value
            cmd.Parameters.Add("PHASTAF", OracleDbType.Date).Value = dtpFechaHasta.Value
            conn.Open()
            DsConsultaMovimi.DETALLEPLANES.Rows.Clear()
            DsConsultaMovimi.DETALLEPLANES.Load(cmd.ExecuteReader)
            conn.Close()
            DETALLEPLANESDataGridView.Focus()
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnFacturar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFacturar.Click
        Try
            Dim mensajeFacturas As String = ""
            Dim mensajeRecibos As String = ""
            Dim tipodoc As String = ""
            Dim numero As String = 0
            If txtDocFactura.Text <> "" And txtDocRecibo.Text <> "" Then
                If DsConsultaMovimi.DETALLEPLANES.Rows.Count > 0 Then
                    Dim cFac As cFacturacion = New cFacturacion(DsConsultaMovimi, txtDocFactura.Text, txtDocRecibo.Text)
                    For Each dr As dsConsultaMovimi.DETALLEPLANESRow In DsConsultaMovimi.DETALLEPLANES.Rows
                        If dr.ACTIVADO = True And numero <> dr.NUMERO Then
                            mensajeFacturas = mensajeFacturas & "," & cFac.facturarPlanes(dr.IDDOCUMENTO, dr.NUMERO)
                            mensajeRecibos = mensajeRecibos & "," & cFac.ReciboPlanes()
                            numero = dr.NUMERO
                        End If
                    Next
                    If mensajeFacturas.Length > 0 Then
                        cFac.modificarEstadoPlanes()
                        MessageBox.Show("Se generaron las siguientes Facturas " + mensajeFacturas, "Facturas", MessageBoxButtons.OK)
                        MessageBox.Show("Se generaron las siguientes Recibos " + mensajeRecibos, "Recibos", MessageBoxButtons.OK)
                    End If
                End If
            Else
                MessageBox.Show("Debe Ingresar los dos documentos", "Documentos", MessageBoxButtons.OK)
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscaDoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaDoc.Click
        Try
            Dim fLDoc As New fListaTipoDocs
            fLDoc.DocEs = "Planes"
            fLDoc.SubClase = "PLA"
            If fLDoc.ShowDialog = Windows.Forms.DialogResult.OK Then
                txtDocumento.Text = fLDoc.idTipoDoc
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim fLDoc As New fListaTipoDocs
            fLDoc.SubClase = "VEN"
            If fLDoc.ShowDialog = Windows.Forms.DialogResult.OK Then
                txtDocFactura.Text = fLDoc.idTipoDoc
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnDocRecibo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDocRecibo.Click
        Try
            Dim fLDoc As New fListaTipoDocs
            fLDoc.SubClase = "REC"
            If fLDoc.ShowDialog = Windows.Forms.DialogResult.OK Then
                txtDocRecibo.Text = fLDoc.idTipoDoc
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class